package bookstore.servlet;

import bookstore.bound.book.BookCreateForm;
import bookstore.bound.book.BookUpdateForm;
import bookstore.bound.order.OrderDisp;
import bookstore.bound.order.StatItem;
import bookstore.bound.user.UserDisp;
import bookstore.bound.user.UserUpdateForm;
import bookstore.manager.AuthManager;
import bookstore.manager.BookManager;
import bookstore.manager.OrderManager;
import bookstore.manager.UserManager;
import bookstore.model.Book;
import bookstore.model.Order;
import bookstore.model.User;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.servlet.http.Part;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/api/admin")
public class AdminServlet {
    protected AuthManager authManager;
    protected UserManager userManager;
    protected BookManager bookManager;
    protected OrderManager orderManager;

    public AdminServlet(AuthManager authManager, UserManager userManager, BookManager bookManager, OrderManager orderManager) {
        System.out.println(String.format("Constructor of %s", this.getClass().getName()));
        this.authManager = authManager;
        this.userManager = userManager;
        this.bookManager = bookManager;
        this.orderManager = orderManager;
    }

    @PostMapping("/book")
    public Map postNewBook(HttpSession session, @RequestBody BookCreateForm bookCreateForm) {
        authManager.requireAdmin(session);
        return bookManager.addNewBook(bookCreateForm);
    }

    @PostMapping("/book/surface/{isbn}")
    public Map postBookSurface(HttpSession session, @PathVariable String isbn, @RequestPart("file") Part filePart) {
        System.out.println(this.getClass().getName());
        authManager.requireAdmin(session);
        return bookManager.setBookSurface(session.getServletContext(), isbn, filePart);
    }

    @GetMapping("/book")
    public ArrayList getBookList(HttpSession session) {
        authManager.requireAdmin(session);
        return bookManager.getAllBooks();
    }

    @PostMapping("/book/{isbn}")
    public Map updateBook(HttpSession session, @PathVariable String isbn, @RequestBody BookUpdateForm bookUpdateForm, BindingResult errors) {
        authManager.requireAdmin(session);
        errors.hasErrors();
        String attr = bookUpdateForm.getAttr();
        if (attr.equals("author")) {
            return bookManager.updateAuthor(isbn, bookUpdateForm.getValue());
        } else if (attr.equals("house")) {
            return bookManager.updateHouse(isbn, bookUpdateForm.getValue());
        } else if (attr.equals("sellPrice")) {
            return bookManager.updateSellPrice(isbn,Integer.parseInt(bookUpdateForm.getValue().replace(".", "").replace("￥", "").trim()));
        } else if (attr.equals("stock")) {
            return bookManager.updateStock(isbn, Integer.parseInt(bookUpdateForm.getValue()));
        } else {
            return null;
        }
    }

    @DeleteMapping("/book/{isbn}")
    public Map deleteBook(HttpSession session, @PathVariable String isbn) {
        authManager.requireAdmin(session);
        return bookManager.delete(isbn);
    }

    @GetMapping("/order")
    public Set<OrderDisp> getOrderList(HttpSession session) {
        authManager.requireAdmin(session);
        return orderManager.getAllOrders();
    }

    @GetMapping("/stat/sale")
    public List<StatItem> getSaleStat(HttpSession session) {
        authManager.requireAdmin(session);
        return orderManager.getSaleStat();
    }

    @GetMapping("/user")
    public Set<UserDisp> getUserList(HttpSession session) {
        authManager.requireAdmin(session);
        return userManager.getAllUsers();
    }

    @PostMapping("/user/{id}")
    public Map updateUser(HttpSession session, @PathVariable Long id, @RequestBody UserUpdateForm userUpdateForm) {
        authManager.requireAdmin(session);
        String type = userUpdateForm.getType();
        if (type.equals("TEL")) {
            return userManager.updateTel(id, userUpdateForm.getTel());
        } else if (type.equals("AVAILABLE")) {
            return userManager.updateAvailable(id, userUpdateForm.isFlag());
        } else {
            return null;
        }
    }

    @DeleteMapping("/user/{id}")
    public Map updateUser(HttpSession session, @PathVariable Long id) {
        authManager.requireAdmin(session);
        return userManager.delete(id);
    }
}
